var app = angular.module("myapp", []);
app.controller("TeachController", function($scope) {
	$scope.teachs = eval(teachs);
});
app.controller("MessageController", function($scope) {
	$scope.messages = eval(messages);
	console.log($scope.messages);
	$scope.mousein = function(row) {
		$scope.selectedRow = row;
	}
	$scope.download = function(row) {
		location.href = "download?fileID=" + $scope.messages[row].id;
	};
	$scope.remove = function(row) {
		var url = "delete/message?id="+$scope.messages[row].messageID;
		console.log("请求地址："+url);
		location.href = url;
	};
	// $scope.remove = function(row) {
	// var url = "delete/message?id=" + $scope.messages[row].messageID;
	// console.log("请求地址：" + url);
	// $http.get(url).success(function(data) {
	// console.log("请求结果：" + data);
	// }).error(function(data, status, headers, config) {
	//
	// });
	//
	//	};
});
$(function() {
	var new_message = $('#new_message_btn');
	var reset_btn = $('#reset_btn');
	var isOpened = false;
	new_message
			.click(function() {
				if (isOpened) {
					$(this)
							.html(
									"<span class=\"glyphicon glyphicon-plus\"></span>&nbsp;新增通知");
				} else {
					$(this)
							.html(
									"<span class=\"glyphicon glyphicon-minus\"></span>&nbsp;取消上传");
					reset_btn.click();
				}
				isOpened = !isOpened;
			});

	var bar = $('.progress-bar');
	var status = $('#status');
	var percentValue = $('#percentValue');
	$('#new_message_file').ajaxForm({
		beforeSend : function() {
			status.empty();
			percentValue.empty();
			var percentVal = '0%';
			percentValue.html(percentVal);
			bar.width(percentVal);
		},
		uploadProgress : function(event, position, total, percentComplete) {
			var percentVal = percentComplete + '%';
			percentValue.html(percentVal);
			bar.width(percentVal);
		},
		success : function() {
			var percentVal = '100%';
			percentValue.html(percentVal);
			bar.width(percentVal);
			status.html("上传完成，正在处理... 请稍等");
		},
		complete : function(xhr) {
			console.log(xhr.responseText);
			var res = eval("(" + eval(xhr.responseText) + ")");
			console.log(res);
			if (res.error == 1) {
				$("#submit-btn").removeAttr("disabled");
				$("#submit-btn").removeClass("disabled");
				percentValue.html('0%');
				bar.width('0%');
				status.html(res.msg);
			} else {
				new_file.click();
				percentValue.html('0%');
				bar.width('0%');
				status.empty();
				var scope = angular.element($("#hello")).scope();
				scope.messages.splice(0, 0, res.message);
				scope.$apply();
			}
		}
	});

});